package com.mxsat.system.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mxsat.common.annotation.ResponseResult;
import com.mxsat.common.result.PlatformResult;
import com.mxsat.system.entity.SysUser;
import com.mxsat.system.service.SysUserService;

@ResponseResult(PlatformResult.class)
@RestController
@RequestMapping("/*")
public class SystemUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@GetMapping("/users")
	public Page<SysUser> users(@RequestParam(name="page",required=true) int page,
	   @RequestParam(name="limit",required=true) int limit,
	   @RequestParam(name="keyword",required=false) String keyword) throws Exception {
		IPage<SysUser> _page = new Page<>();
		_page.setPages(page);
		_page.setSize(limit);
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("keyword", keyword);
		return (Page<SysUser>) sysUserService.findSysUsersByPage(_page,condition);
	}
	
	@GetMapping("/user/{uid}")
	public SysUser user(@PathVariable Long uid) throws Exception{
		return sysUserService.findSysUserById(uid);
	}
	
	@PostMapping("/user")
	public boolean addUser(SysUser user) throws Exception{
		return sysUserService.addSysUser(user);
	}
	
	@PutMapping("/user")
	public boolean modifyUser(SysUser user) throws Exception{
		return sysUserService.modifySysUserById(user);
	}
	
	@DeleteMapping("/user/{uid}")
	public boolean removeUser(@PathVariable Long uid) throws Exception{
		return sysUserService.removeSysUserById(uid);
	}
	
	@GetMapping("/user/name/{userName}")
	public SysUser findUserByName(@PathVariable String userName) throws Exception{
		return sysUserService.findUserByName(userName);
	}
}
